package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.ConditionRole;
import com.ebring.shareEducation.bean.Role;
import com.ebring.shareEducation.bean.User;
import com.ebring.shareEducation.bean.response.UserRsp;
import com.ebring.shareEducation.service.RoleService;
import com.ebring.shareEducation.dao.RoleMapper;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    //查询所有的角色
    @Override
    public RspMsg<List<Role>> selectRole() {
        Map<String, Object> map = new LinkedHashMap<>();
        RspMsg rspMsg = new RspMsg();
        List<Role> roleList = roleMapper.selectAll();
        if (roleList.isEmpty() || roleList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到数据");
            return rspMsg;

        }



        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("roleList", roleList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }


    //查询所有的角色
    @Override
    public RspMsg<List<ConditionRole>> selectCRByUserRole(String roleId) {
        Map<String, Object> map = new LinkedHashMap<>();
        RspMsg rspMsg = new RspMsg();
        List<ConditionRole> roleList = roleMapper.selectCRByUserRole(roleId);
        if (roleList.isEmpty() || roleList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到数据");
            return rspMsg;
        }
        map.put("roleList", roleList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }
    /**
     * 查询角色的下级角色
     */
    @Override
    public RspMsg<List<Role>> selectRoleByUserId(String userId) {
        RspMsg rspMsg = new RspMsg();
        List<Role> roleList = roleMapper.selectRoleByUserId(userId);
        if (roleList.isEmpty() || roleList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到数据");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setResult(roleList);
        return rspMsg;
    }

    //修改角色
    @Override
    public RspMsg<Role> updateRole(Role role) {
        RspMsg rspMsg = new RspMsg();
        int i = roleMapper.updateByPrimaryKeySelective(role);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("修改失败");
            return rspMsg;
        }
        rspMsg.setOK();
        return rspMsg;

    }
    //新增加角色
    @Override
    public RspMsg<Role> insetRole(Role role) {
        RspMsg rspMsg = new RspMsg();

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = df.format(new Date());
        role.setRoleCreateDate(time);
        List<Role> role1 = roleMapper.selectAll();
        Integer size=role1.size();
        Integer statuss = size + 1;
        String status = statuss.toString();
        role.setRoleStatus(status);
        int i = roleMapper.insertSelective(role);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("新增失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("新增成功");

        return rspMsg;

    }
    //模糊查询角色管理
    @Override
    public RspMsg<List<Role>> select(String roleId, String name) {
        Example example = new Example(Role.class);
        if ((name != null && !"".equals(name))&&(roleId != null && !"".equals(roleId))){
            example.createCriteria().andLike("name", "%"+name+"%").andLike("roleId", "%"+roleId+"%");
        }
        if (name != null && !"".equals(name)){
            example.createCriteria().andLike("name", "%"+name+"%");
        }
        if (roleId != null && !"".equals(roleId)){
            example.createCriteria().andLike("roleId", "%"+roleId+"%");
        }

        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List roleList = roleMapper.selectByExample(example);
        if (roleList.isEmpty() || roleList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关用户");
            return rspMsg;
        }

        PageInfo pageInfo = new PageInfo<>(roleList);

        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("roleList", roleList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }

    // 批量删除用户
    @Override
    public RspMsg<Role> DeleteRole(String roleId) {

        RspMsg rspMsg = new RspMsg();
        int sum = 0;
        sum=roleMapper.deleteByPrimaryKey(roleId);
        if (sum > 0) {
            rspMsg.setOK();
            rspMsg.setMsg("删除成功");

            rspMsg.setResult(sum);
            return rspMsg;
        }

        rspMsg.setError();
        rspMsg.setMsg("删除失败");
        return rspMsg;
    }





}

